<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/31
 * Time: 16:49
 */

namespace Admin\Controller;
use Common\Model\OrderModel;


use Common\Controller\AdminbaseController;

class PopularizeController extends AdminbaseController
{
    protected $promotersModel;
    protected $promotion_registrationModel;
    protected $memberModel;
    protected $orderModel;

    function _initialize()
    {
        $this->promotersModel = D("promoters");
        $this->promotion_registrationModel = D("promotion_registration");
        $this->memberModel = D("member");
        $this->orderModel  = new OrderModel();
    }

    //查询自己的设置推广人情况
    public function index(){
        $session_admin_id=session('ADMIN_ID');
        if($session_admin_id == 2){
            $this->error("您没有访问的权限！");
        }
        $info = $this->promotersModel->where(array("user_id"=>$session_admin_id))->find();
        if(!$info){
            $this->error('工作人员不存在！');
        }
        $post = I('');
        $where['phone'] = $info['phone_mob'];
        //查看邀请次数
        $p = $post['p']?$post['p']:1;
        $pageSize = 20;

        $count = $this->promotion_registrationModel
            ->where($where)->order('id desc')->count();
        $promotionRegistrationInfo = $this->promotion_registrationModel
            ->where($where)->limit(($p-1)*$pageSize,$pageSize)->order('id desc')->select();

        $time = time();
        $num = 0;
        $arr = array();

        foreach($promotionRegistrationInfo as $k=>$v){
            $promotionRegistrationInfo[$k]['start_time'] = $v['end_time'] - 60*60*24;
            if($v['end_time'] > $time && $v['status'] == 1){
                $promotionRegistrationInfo[$k]['status_value'] = '未注册';
            }elseif($v['status'] == 2){
                $promotionRegistrationInfo[$k]['status_value'] = '已注册';
            }else{
                $promotionRegistrationInfo[$k]['status_value'] = '已失效';
            }
            if($v['create_time'] == 0){
                $promotionRegistrationInfo[$k]['time_value'] = '无';
            }else{
                $promotionRegistrationInfo[$k]['time_value'] = date('Y-m-d H:i:s',$v['create_time']);
            }
            $promotionRegistrationInfo[$k]['member_total'] = 0;
            if($v['status'] == 2){
                $num ++;
                $memberInfo = $this->memberModel->where(array("phone_mob"=>$v['invited_phone']))->find();
                $promotionRegistrationInfo[$k]['member_total'] = $this->memberModel->where(array("second_class"=>$memberInfo['user_id'],'user_id'=>array("neq",$memberInfo['user_id'])))->count();
            }
        }

        //邀请成功率
        $invited_bibi = (sprintf("%.2f",$num / $count) * 100).'%';

        $this->assign('count',$count);
        $this->assign('num',$num);
        $this->assign('invited_bibi',$invited_bibi);
        $this->assign('list',$promotionRegistrationInfo);
        $page = $this->page($count,$pageSize);
        $this->assign('page',$page->show("Admin"));
        $this->display();
    }
    //查看推广人的推广情况
    public function memberlist(){
        $store_id = I("store_id");
        $keyword = trim(I("post.keyword"));
        $start_time = I("post.start_time");
        $end_time = I("post.end_time");

        if(!$store_id){
            $this->error('信息传递错误!');
        }
        if(isset($keyword) && !empty($keyword)){
            $where['phone_mob'] = $keyword;
            $this->assign('keyword',$keyword);
        }
        if($start_time){
            $s_time = strtotime($start_time);
            $cond['add_time'] = array("egt",$s_time);
            if($end_time){
                $e_time = strtotime($end_time);
                $cond['add_time'] = array("between",array($s_time,$e_time));
            }
        }else if($end_time){
            $e_time = strtotime($end_time);
            $cond['add_time'] = array("elt",$e_time);
        }

        $where['second_class'] = $store_id;
        $list = $this->memberListWhere($where,'reg_time desc');

        //遍历查出的所有符合条件的会员进行统计下单金额及总额
        $totalOrder = 0.00;         //订单总额
        $finishOrderTotal = 0.00;   //完成订单金额
        $countOrder = 0;            //下单总数
        $finishOrderCount = 0;      //已完成订单数
        foreach($list as $k=>$v){
            $cond['buyer_id'] = $v['user_id'];
            $list[$k]['countOrder'] = $this->orderModel->where($cond)->count();
            $list[$k]['totalOrder'] = $this->orderModel->where($cond)->sum('goods_amount')?$this->orderModel->where($cond)->sum('goods_amount'):0;
            $countOrder += $list[$k]['countOrder'];
            $totalOrder += $list[$k]['totalOrder'];

            //已完成的
            $cond['status'] = 40;
            $list[$k]['finishOrderCount'] = $this->orderModel->where($cond)->count();
            $list[$k]['finishOrderTotal'] = $this->orderModel->where($cond)->sum("goods_amount")?$this->orderModel->where($cond)->sum("goods_amount"):0;
            $finishOrderCount += $list[$k]['finishOrderCount'];
            $finishOrderTotal += $list[$k]['finishOrderTotal'];
        }
        $info = $this->memberModel->where(array("user_id"=>$store_id))->find();
        $this->assign('info',$info);
        $this->assign('list',$list);
        $this->assign('totalOrder',$totalOrder);
        $this->assign('finishOrderTotal',$finishOrderTotal);
        $this->assign('countOrder',$countOrder);
        $this->assign('finishOrderCount',$finishOrderCount);
        $this->assign('total',count($list)-1);
        $this->display();
    }

    public function orderList(){
        $user_id = I("post.user_id");
        if(!$user_id){
            echo json_encode(array("code"=>-1));
            exit();
        }
        $cond['ecm_order.buyer_id'] = $user_id;
        $con = array(
            'condition' => $cond,
            'field' => 'ecm_order.* ,ecm_order_goods.specification, ecm_order_goods.price, ecm_order_goods.tax_price, ecm_order_goods.goods_name,ecm_member.*',
            'order' => 'order_id desc'
        );
        $res = $this->orderModel ->getOrderList($con);
        if(empty($res)){
            echo json_encode(array("code"=>-1));
            exit();
        }
        foreach($res as &$v){
            $v['add_time'] = date("Y-m-d H:i:s",$v['add_time']);
        }
        echo json_encode(array('code'=>1,'data'=>$res));
        exit();

    }
}